class Axios{
    constructor() {
    }
    dealParams(params){
        let keys = Object.keys(params);
        let values = Object.values(params);
        let str = keys.reduce((previous,currentValue,index)=>{
            return previous +  keys[index] + "=" +  values[index] + "&&"
        },"?");
        return str.slice(0,-2);
    }
    get({url, params = {}}){
        return new Promise((resolve, reject)=>{
            let xhr = this.createXhr(function (){
                let list = JSON.parse(xhr.responseText)
                resolve(list)
            })
            url = url + this.dealParams(params)
            xhr.open("get",url,true)
            xhr.send(null)

        })
    }
    createXhr(callback){
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = ()=>{
            if(xhr.readyState !== 4){
                return
            }
            let status = xhr.status;
            if((status >= 200 && status < 300) || status === 304){
                callback()
            }
        }
        return xhr;
    }
    post({url,params={}}){
        return new Promise((resolve, reject)=>{
            let xhr = this.createXhr(function (){
                let list = JSON.parse(xhr.responseText)
                resolve(list)
            })
            xhr.open("post",url,true)
            xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8")
            xhr.setRequestHeader("Origin","http://127.0.0.1:5000")
            xhr.send(JSON.stringify(params))

        })
    }
}
